<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>计算公式管理</title>
  <link rel="stylesheet" href="${ctx}/static/admin/layui/css/layui.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/sidebar.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/switch.css">
</head>
  <body>
	<div class="layui-card-body">
		<#button permission="" id="add" size="layui-btn-sm" textName="新增" event="add"/>
		<table id="calculatetable" lay-filter="calculate"></table>
	</div>
  </body>
</html>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<script src="${ctx}/static/admin/js/common.js"></script>
<script type="text/html" id="toolbar">
<#button permission="" iconCls="layui-btn" size="layui-btn-xs" textName="编辑" event="edit"/>
<#button permission="" iconCls="layui-btn-danger" size="layui-btn-xs" textName="删除" event="del"/>
</script>
<script type="text/javascript">
	var ctx = '${ctx}', tablename = '${tablename!}';
    var params = {}, tableIns;
    layui.use(['table','layer','jquery','element','sidebar','form'], function(){
        var $ = jQuery = layui.$;
        var table = layui.table;
        var layer = layui.layer;
        var element = layui.element;
        var sidebar = layui.sidebar;
        var form = layui.form;
        tableIns = table.render({
            elem: '#calculatetable',
            height:'full-113',
            size:'sm',
            url: ctx+'/sys_admin/calculate/getList?moduleid=${moduleid!}',
            page: true,
            even: true,
            cols: [[
                {type: 'numbers',fixed: 'left'},
                {field: 'id', title: 'ID',fixed: 'left', align:'center',width:180, hide:true},
                {field: 'column_comment', title: '字段名称',fixed: 'left', align:'center',width:200},
                {field: 'calculate', title: '计算公式', align:'center'},
                {title: '操作', width: 450, fixed: 'right', align:'center',toolbar: '#toolbar'}
            ]],
            done:function(res, curr){
                params['curr'] = curr;
            }
        });
        table.on('tool(calculate)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('确定删除行么？', function(index){
                    $.post(ctx+'/sys_admin/calculate/delete', {"id":data.id}, function(result) {
                        if (result.success) {
                            obj.del();
                            layer.msg("删除成功!")
                            layer.close(index);
                        } else {
                            layer.msg(result.msg);
                        }
                    }, 'json');
                });
            } else if(obj.event === 'edit'){
                parent.layer.open({
                    type: 2,
                    area: ['50%', '90%'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: ctx+'/sys_admin/calculate/edit?id='+data.id,
                    btn: ['提交', '重置','取消'],
                    yes: function(index, layero){
                        var iframeWin = layero.find('iframe')[0];
                        iframeWin.contentWindow.caltable = tableIns;
                        iframeWin.contentWindow.params = params;
                        layero.find('iframe').contents().find('#submit-btn').click();
                    },
                    btn2: function(index, layero){
                        var iframeWin = layero.find('iframe')[0];
                        iframeWin.contentWindow.caltable = tableIns;
                        layero.find('iframe').contents().find('#reset-btn').click();
                        return false;
                    },
                    btn3: function(index, layero){
                        var index = layer.getFrameIndex(window.name);
                        layer.close(index);
                    }
                });
            }
        });

        $("#add").on('click',function(){
            parent.layer.open({
                type: 2,
                area: ['50%', '90%'],
                fixed: false, //不固定
                maxmin: true,
                content: ctx+'/sys_admin/calculate/form?moduleid=${moduleid!}',
                btn: ['提交', '重置','取消'],
                yes: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.caltable = tableIns;
                    iframeWin.contentWindow.params = params;
                    layero.find('iframe').contents().find('#submit-btn').click();
                },
                btn2: function(index, layero){
                    var iframeWin = layero.find('iframe')[0];
                    iframeWin.contentWindow.caltable = tableIns;
                    layero.find('iframe').contents().find('#reset-btn').click();
                    return false;
                },
                btn3: function(index, layero){
                    var index = layer.getFrameIndex(window.name);
                    layer.close(index);
                }
            });
        });

    });
</script>